package com.stageauthserver.authserver.service.impl;


import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageauthserver.authserver.service.UserService;
import com.stagecommon.common.mapper.UserMapper;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yxl
 * @since 2021-11-09
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public RespBean<User> checkLogin(String phone) {
        if (!Objects.equals(null, userMapper.findByPhone(phone))) {
            StpUtil.login(phone);
            User user = userMapper.selectOne(new QueryWrapper<User>().select("user_img","user_id", "username").eq("phone_num", phone));
            List<Integer> roleIdList = userMapper.listRoleId(user.getUserId());
            user.setRoleIdList(roleIdList);
            return RespBean.loginSuccessReturnToken("登录成功", 200, user, StpUtil.getTokenValue());
        } else {
            redisTemplate.opsForValue().increment("loginTimes" + phone, 1);
            return RespBean.operationFail("登录失败");
        }
    }



}
